package cn.cagurzhan.controller;

import cn.cagurzhan.common.constant.CacheConstants;
import cn.cagurzhan.common.constant.CommandNameConstants;
import cn.cagurzhan.common.constant.CommandPropertiesConstants;
import cn.cagurzhan.common.constant.ServiceConstant;
import cn.cagurzhan.domain.R;
import cn.cagurzhan.domain.dto.CommandResultDto;
import cn.cagurzhan.domain.dto.DeviceInfoUploadDto;
import cn.cagurzhan.domain.entity.HomeLog;
import cn.cagurzhan.domain.entity.OperLog;
import cn.cagurzhan.domain.vo.SafetyVo;
import cn.cagurzhan.domain.vo.ShouyeVo;
import cn.cagurzhan.service.AgriService;
import cn.cagurzhan.service.CacheService;
import cn.cagurzhan.service.CommandService;
import cn.cagurzhan.service.IoTService;
import cn.cagurzhan.utils.BeanCopyUtils;
import cn.cagurzhan.utils.ServletUtils;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Arg;
import org.omg.CORBA.ARG_IN;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.Servlet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 智能家居控制器
 * @author AjaxZhan
 */
@RestController
@RequestMapping("/api/command")
@RequiredArgsConstructor
@Slf4j
public class CommandController {

    /**
     * 华为云服务
     */
    private final IoTService ioTService;

    /**
     * 命令服务
     */
    private final CommandService commandService;

    /**
     * 农业服务，用于语音控制
     */
    private final AgriService agriService;

    private final CacheService cacheService;

    /**
     * 设备ID
     */
    private static final String DEVICE_ID_HOME = "65ed96477bdccc0126c79cb0_ESP32_WROOM";

    /**
     * 获取所有日志消息
     */
    @GetMapping("/log/list")
    public R<List<OperLog>> listLog(){
        return commandService.listLog();
    }


    /**
     * 首页信息展示
     */
    @GetMapping("/shouye")
    public R<ShouyeVo> getShouyeInfo(){
        return commandService.getShouyeInfo();
    }

    /**
     * 安全页面数据展示
     */
    @GetMapping("/safety")
    public R<HomeLog> getSafetyVo(){
        return commandService.getSafetyVo();
    }

    /**
     * 指令：开门
     * @param status 0关门 1开门
     */
    @PostMapping ("/door_open/{status}")
    public R<Void> doorOpen(@PathVariable("status") Integer status){
        return commandService.doorOpen(status);
    }

    /**
     * 指令：打开车库门
     * @param status 0关门 1开门
     */
    @PostMapping("/car_door_open/{status}")
    public R<Void> carDoorOpen(@PathVariable("status") Integer status){
        return commandService.carDoorOpen(status);
    }

    /**
     * 指令：车库内门
     * @param status 0关门 1开门
     */
    @PostMapping("/Caridoor/{status}")
    public R<Void> caridoor(@PathVariable("status") Integer status){
        return commandService.caridoor(status);
    }

    /**
     * 指令：控制LED
     */
    @PostMapping("/LED_control/{which}/{light}")
    public R<Void> ledControl(@PathVariable("which")Integer which, @PathVariable("light")Integer light){
        return commandService.ledControl(which,light);
    }

    /**
     * 指令：控制窗户
     */
    @PostMapping("/window_control/{angle}")
    public R<Void> windowControl(@PathVariable("angle")Integer angle){
        return commandService.windowControl(angle);
    }


    /**
     * 指令：火警
     */
    @PostMapping("/alarm/{status}")
    public R<Void> alarm(@PathVariable Integer status){
        return commandService.alarm(status);
    }

    /**
     * 改变房间状态
     * 0
     */
    @PostMapping ("/home/{status}")
    public R<Void> changeHomeStatus(@PathVariable("status") Integer status){
        return commandService.changeHomeStatus(status);
    }

    /**
     * 接收语音转发的消息
     */
    @PostMapping("/collect")
    public R<Void> receiveCommand(@RequestBody DeviceInfoUploadDto dto){
        log.info("【转发】：收到转发消息，内容如下:{}",JSON.toJSONString(dto));
        Map<String, Object> properties = dto.getNotify_data().getBody().getServices().get(0).getProperties();
        Integer v_control = (Integer) properties.get("v_control");

        // 判断是否开启语音功能
        String s = cacheService.get("yuyin");
        if(s.equals("0")){
            // 关闭功能
            return R.fail("语音功能已经关闭");
        }

        // 前方大量if-else屎山代码，因硬件写死只能如此
        if(v_control.equals(1)){
            // 开 客厅白灯
            return agriService.guangai(1,1023);
            // return commandService.ledControl(CommandPropertiesConstants.LED_KETING_WHITE,1023);
        }else if(v_control.equals(2)){
            // 关 客厅白灯
            return agriService.guangai(1,0);
            // return commandService.ledControl(CommandPropertiesConstants.LED_KETING_WHITE,0);
        }else if(v_control.equals(3)){
            // 开 卧室灯
            return agriService.guangai(0,1023);
            // return commandService.ledControl(CommandPropertiesConstants.LED_WOSHI,1023);
        }else if(v_control.equals(4)){
            // 关 卧室灯
            return agriService.guangai(0,0);
            // return commandService.ledControl(CommandPropertiesConstants.LED_WOSHI,0);
        }else if(v_control.equals(5)){
            // 开 厨房灯
            return agriService.shade(100);
            // return commandService.ledControl(CommandPropertiesConstants.LED_CHUFANG,1023);
        }else if(v_control.equals(6)){
            // 关 厨房灯
            return agriService.shade(80);
            // return commandService.ledControl(CommandPropertiesConstants.LED_CHUFANG,0);
        }else if(v_control.equals(7)){
            // 开 浴室灯
            return agriService.shade(90);
            // return commandService.ledControl(CommandPropertiesConstants.LED_WEISHENGJIAN,1023);
        }else if(v_control.equals(8)){
            // 关 浴室灯
            return agriService.light(0,1023);
            // return commandService.ledControl(CommandPropertiesConstants.LED_WEISHENGJIAN,0);
        }else if(v_control.equals(9)){
            // 暗 客厅
            return agriService.light(0,0);
            // return commandService.ledControl(CommandPropertiesConstants.LED_KETING_YELLOW,500);
        }else if(v_control.equals(10)){
            // 亮 客厅
            return agriService.light(0,512);
            // return commandService.ledControl(CommandPropertiesConstants.LED_KETING_YELLOW,1023);
        }else if(v_control.equals(11)){
            // 暗 卧室
            return agriService.light(0,1023);
            // return commandService.ledControl(CommandPropertiesConstants.LED_WOSHI,500);
        }else if(v_control.equals(12)){
            // 亮 卧室
            return agriService.light(1023,0);
            // return commandService.ledControl(CommandPropertiesConstants.LED_WOSHI,1023);
        }else if(v_control.equals(13)){
            // 开 窗户
            return agriService.light(0,0);
            // return commandService.windowControl(1);
        }else if(v_control.equals(14)){
            // 关 窗户
            return agriService.light(512,0);
            // return commandService.windowControl(0);
        }else if(v_control.equals(15)){
            // 开 大门
            return agriService.light(1023,0);
            // return commandService.doorOpen(1);
        }else if(v_control.equals(16)){
            // 关 大门
            return agriService.fans(1023);
            // return commandService.doorOpen(0);
        }else if(v_control.equals(17)){
            // 开 车库外门
            return agriService.fans(0);
            // return commandService.carDoorOpen(1);
        }else if(v_control.equals(18)){
            // 关 车库外门
            return commandService.carDoorOpen(0);
        }
        // else if(v_control.equals(21)){
        //     // 开 客厅暖灯
        //     return commandService.ledControl(CommandPropertiesConstants.LED_KETING_YELLOW,1023);
        // }else if(v_control.equals(22)){
        //     // 关 客厅暖灯
        //     return commandService.ledControl(CommandPropertiesConstants.LED_KETING_YELLOW,0);
        // }else if(v_control.equals(23)){
        //     // 暗 客厅白灯
        //     return commandService.ledControl(CommandPropertiesConstants.LED_KETING_WHITE,500);
        // }else if(v_control.equals(24)){
        //     // 亮 客厅白灯
        //     return commandService.ledControl(CommandPropertiesConstants.LED_KETING_WHITE,1023);
        // }else if(v_control.equals(25)){
        //     // 关闭警报
        //     return commandService.alarm(1);
        // }
        else{
            return R.ok();
        }
    }

    /**
     * 指纹识别
     */
    @PostMapping("/collect2")
    public R<Void> receiveCommandFromFinger(@RequestBody DeviceInfoUploadDto dto){
        log.info("【转发】：/collect2 收到转发消息，内容如下:{}",JSON.toJSONString(dto));
        Map<String, Object> properties = dto.getNotify_data().getBody().getServices().get(0).getProperties();
        log.info("【转发】：接收到属性{}",properties.toString());
        return commandService.finger(properties);
    }

    /**
     * 人脸识别
     */
    @PostMapping("/collect3")
    public R<Void> receiveCommandFromFace(@RequestBody DeviceInfoUploadDto dto){
        log.info("【转发】：/collect3 收到转发消息，内容如下:{}",JSON.toJSONString(dto));
        Map<String, Object> properties = dto.getNotify_data().getBody().getServices().get(0).getProperties();
        log.info("【转发】：/collect3 接收到属性{}",properties.toString());
        return commandService.face(properties);
    }


}
